/**
 * 手写签名板公用js
 */

var url = "ws://127.0.0.1:12343";
var websocket;
var connected = false;

/**
 * 初始化webSocket连接
 * @param callback
 * @param value
 * @constructor
 */
function ConnectServer(callback, value) {

    if ('WebSocket' in window) {
        websocket = new WebSocket(url);
    } else if (window.WebSocket) {
        websocket = new WebSocket(url);
    } else if ('MozWebSocket' in window) {
        websocket = new MozWebSocket(url);
    } else {
        alert("浏览器版本太低！请使用Chrome、Firefox、IE10+浏览器！");
    }

    websocket.onopen = function () {
        connected = true;
        callback(value);
    }
    websocket.onclose = function (e) {
        connected = false;
    }
    websocket.onmessage = function (e) {
        onMessage(e);
    }
    websocket.onerror = function (e) {
        alert("未连接websocket服务器，请确保已运行服务端！!!!")
    };

}

/**
 * 接收服务器消息
 * @param e
 */
function onMessage(e) {
    var jsonObj = eval('(' + e.data + ')');
    //通用回调处理函数 jsonObj 遵循文档返回数据格式要求
    //singProduceMessage(jsonObj);
	  console.log(e)
    var result = JSON.parse(e.data);
    var success = result.success;
	var show = document.getElementById("show");

    if(success == "1"){
		//var signData = data.signData;
        var functionName = result.functionName;
        var pic = document.getElementById("pic");
		if (functionName == "ZCOutputImageBase64AB")
		{
			pic.src = "data:image/png;base64," + result.Base64;
            this.data.qmdata = pic.src;
		}
		if(functionName == "ZCRealTimeOutputImageBase64AB")
		{
			var picsign = document.getElementById("picsign");
			picsign.src = "data:image/png;base64," + result.Base64;
            this.data.qmdata = picsign.src;
		}
        
		else 
		{
			show.value = show.value +'\n' + e.data;
		}

    }
	else{
        //alert('设备处理失败，失败信息：'+result.errorMsg);
    }
}

/**
 * 向服务器发送信息的共享方法
 * @param jsonStr
 */
function sendMessage(jsonStr) {
    connected ? websocket.send(jsonStr) : alert("未连接websocket服务器，请确保已运行服务端！")
}


/******************* 以下为签名平板函数 *************************/

/**
 * 启动签字设备
 * @constructor
 */
function ZCStartDeviceAB() {
    var data = JSON.stringify({'function': 'ZCStartDeviceAB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 退出签名模式
 * @constructor
 */
function ZCEndSignAB() {
    var data = JSON.stringify({'function':'ZCEndSignAB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 初始化设备并进入签名模式
 * @param x 在签字板中的x
 * @param y 在签字板中的y
 * @param width 签字窗体宽度
 * @param height 签字窗体高度
 * @constructor
 */
function ZCBeginSignAB(x,y,width,height) {
    var data = JSON.stringify({'function':'ZCBeginSignAB','x':x,'y':y,'width':width,'height':height});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 停止设备
 * @constructor
 */
function ZCStopDeviceAB() {
    var data = JSON.stringify({'function':'ZCStopDeviceAB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 将当前笔迹保存到Base64串中,并自动裁剪掉白边
 * @constructor
 */
function ZCOutputImageBase64AB() {
    var data = JSON.stringify({'function':'ZCOutputImageBase64AB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 签名版屏幕网页推送
 * @param url 推送地址
 * @constructor
 */
function ZCShowHtmlAB(url) {
    var data = JSON.stringify({'function':'ZCShowHtmlAB','url': url});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

function ZCCloseHtmlAB() {
    var data = JSON.stringify({ 'function': 'ZCCloseHtmlAB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 设置笔模式
 * @param mode 笔模式
 * @constructor
 */
function ZCSetModeAB(mode) {
    var data = JSON.stringify({'function':'ZCSetModeAB','mode': mode});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}


/**
 * 设置取消按键
 * @param cancleBtn 设置cancle button
 * @constructor
 */
function ZCSetCanceBtnAB(cancleBtn) {
    var data = JSON.stringify({'function':'ZCSetCanceBtnAB','cancleBtn': cancleBtn});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 进入评价名模式
 * @param x 在签字板中的x
 * @param y 在签字板中的y
 * @param width 签字窗体宽度
 * @param height 签字窗体高度
 * @constructor
 */
function ZCBeginEvaluationAB(x,y,width,height) {
    var data = JSON.stringify({'function':'ZCBeginEvaluationAB','x':x,'y':y,'width':width,'height':height});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}

/**
 * 结束评价
 * @constructor
 */
function ZCEndEvaluationAB() {
    var data = JSON.stringify({'function':'ZCEndEvaluationAB'});
    connected ? sendMessage(data) : ConnectServer(sendMessage, data)
}
 